package databaseHandling;

import interfaces.SalgInterface;
import interfaces.VareInterface;
import interfaces.VareSalgInterface;
import java.rmi.RemoteException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.Bruger;
import model.Salg;
import model.Vare;
import model.VareGruppe;
import model.VareSalg;

/**
 *
 * @author Maja
 */
public abstract class SalgsHandler {

    public static void opretSalg(int brugerid) throws SQLException {
        DatabaseHandler.getInstance().execute("CALL opretSalg (" + brugerid + ")");
    }

    public static void tilføjVareTilSalg(int vareid, int salgsid, int antal, int salgsPris) throws SQLException {
        DatabaseHandler.getInstance().execute("CALL tilfojVareTilSalg(" + vareid + ", " + salgsid + ", " + antal + ", " +salgsPris+")");
    }

    private static ArrayList<VareSalgInterface> getVareSalgFraSalg(int salgsid) throws SQLException, RemoteException{
        ArrayList vareSalgsListe = new ArrayList<>(); 
        ResultSet rs = DatabaseHandler.getInstance().select("CALL HentVareSalgFraSalg(" + salgsid +")");
        while(rs.next()){            
            VareGruppe vareGruppe = new VareGruppe(rs.getInt("VareGruppeid"), rs.getString("gruppeNavn"));
            VareInterface vare = new Vare(rs.getInt("Vareid"),rs.getString("titel"), rs.getString("beskrivelse"),
                    rs.getInt("pris"), rs.getInt("antalReserveret"), rs.getInt("antalLager"), vareGruppe);
            VareSalg varesalg = new VareSalg(rs.getInt("VareSalgsid"),rs.getInt("antal"), rs.getInt("salgsPris"), vare);
            vareSalgsListe.add(varesalg);
        }
        return vareSalgsListe;
    }
    
    public static Salg getAktivtSalg(int brugerid) throws SQLException, RemoteException {
        Salg salg = null;
        ResultSet rs = DatabaseHandler.getInstance().select("CALL HentAktivtSalg (" + brugerid + ")");
        if (rs.next()) {
            Bruger bruger = new Bruger(rs.getInt(brugerid), rs.getString("brugerNavn"), rs.getString("password"));
            salg = new Salg(rs.getInt("id"), rs.getInt("totalPris"), rs.getString("salgsDato"), rs.getInt("status"), bruger);
            salg.setVareSalgsListe(getVareSalgFraSalg(salg.getId()));
        }
        
        return salg;
    }

    public static void gennemfoerSalg(SalgInterface salg) throws SQLException, RemoteException {
        StringBuffer sb = new StringBuffer();
        sb.append("Start Transaction; ");
        sb.append("CALL gennemforSalg("+ salg.getId()+")");
        for (VareSalgInterface vareSalg : salg.getVareSalgsListe()) {
        sb.append("CALL updateVareAntal()");
        DatabaseHandler.getInstance().execute("CALL gennemforSalg(" + salg.getId() + "," + salg.getTotalPris() + ")");
        }
    }

    public static void annullerSalg(SalgInterface salg) throws SQLException, RemoteException {
        DatabaseHandler.getInstance().execute("CALL annullerSalg (" + salg.getId() + ")");
    }

}
